package com.cgtong.android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.text.TextUtils;

import com.blankj.utilcode.util.TimeUtils;
import com.cgtong.android.R;
import com.cgtong.android.bean.NewsBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.widget.GlideApp;

import java.util.List;

/**
 * Created by Sky.Bing on 2017/10/18.
 */

public class NewsRecyclerAdapter extends RecyclerView.Adapter<NewsRecyclerAdapter.NewsViewHolder> implements View.OnClickListener {
    private Context mContext;
    private ItemClickListener itemClick;
    private List<NewsBean> newsBeanList;

    public NewsRecyclerAdapter(Context context, List<NewsBean> newsBeanList, ItemClickListener onClickListener) {
        this.mContext = context;
        this.newsBeanList = newsBeanList;
        this.itemClick = onClickListener;
    }

    @Override
    public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        NewsViewHolder vh = new NewsViewHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_home_news, parent, false));
        vh.convertView.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(NewsViewHolder holder, int position) {
        final NewsBean newsBean = newsBeanList.get(position);
        holder.convertView.setTag(newsBean);
        holder.newsTitle.setText(newsBean.getTitle());
        holder.newsTime.setText(TimeUtils.date2String(TimeUtils.string2Date(newsBean.getPublish_date(), ConstantManager.YYYY_MM_DD_FORMAT), ConstantManager.YYYY_MM_DD_FORMAT));
        holder.sendOrigin.setText(newsBean.getSources());
        if(TextUtils.isEmpty(newsBean.getPicture())){
            holder.newsImage.setImageResource(R.drawable.icon_default_image_empty);
        }else{
            GlideApp.with(mContext).load(newsBean.getPicture()).placeholder(R.drawable.icon_default_loading).error(R.drawable.icon_default_error).centerCrop().into(holder.newsImage);
        }
    }

    @Override
    public int getItemCount() {
        return this.newsBeanList.size();
    }

    class NewsViewHolder extends RecyclerView.ViewHolder {
        public ImageView newsImage;
        public TextView newsTitle;
        public TextView sendOrigin;
        public TextView newsTime;
        public View convertView;

        public NewsViewHolder(View itemView) {
            super(itemView);
            convertView = itemView;
            newsImage = (ImageView) itemView.findViewById(R.id.news_image);
            newsTitle = (TextView) itemView.findViewById(R.id.news_title);
            sendOrigin = (TextView) itemView.findViewById(R.id.send_origin);
            newsTime = (TextView) itemView.findViewById(R.id.news_time);
        }
    }

    @Override
    public void onClick(View v) {
        if (null != itemClick) {
            itemClick.onItemClick(v, (NewsBean) v.getTag());
        }
    }

    public interface ItemClickListener {
        void onItemClick(View v, NewsBean newsBean);
    }
}